<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Password Matching Example</title>
    <style>
        .error-message {
            color: red;
        }
    </style>
</head>
<body>

<form id="registrationForm">
    <label for="username">用户名:</label>
    <input type="text" id="username" name="username" required><br>

    <label for="password">密&nbsp;&nbsp;码:</label>
    <input type="password" id="password" name="password" required><br>

    <label for="confirmPassword">确认密码:</label>
    <input type="password" id="confirmPassword" name="confirmPassword" required>
    <span id="passwordError" class="error-message"></span><br>

    <input type="submit" value="提交">
</form>

<script>
    document.getElementById('registrationForm').addEventListener('submit', function (event) {
        var password = document.getElementById('password').value;
        var confirmPassword = document.getElementById('confirmPassword').value;
        var passwordError = document.getElementById('passwordError');

        if (password !== confirmPassword) {
            passwordError.textContent = '密码不匹配';
            event.preventDefault(); // Prevent form submission
        } else {
            passwordError.textContent = ''; // Clear error message
        }
    });
</script>

</body>
</html>
